package com.ln.lnzw.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.ln.lnzw.R;
import com.ln.lnzw.base.BaseActivity;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by wuwei on 2018/4/26.
 * 意见反馈
 */

public class FeedbackActivity extends BaseActivity {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.cet_yijianfankui)
    EditText cetYijianfankui;
    @BindView(R.id.tv_zishu)
    TextView tvZishu;
    @BindView(R.id.tv_fasong)
    TextView tvFasong;
    private int MAX_LENGTH = 100;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feedback);
        ButterKnife.bind(this);
        InputFilter inputFilter = new InputFilter() {
            Pattern emoji = Pattern.compile("[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]",
                    Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE);

            @Override
            public CharSequence filter(
                    CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                Matcher emojiMatcher = emoji.matcher(source);
                if (emojiMatcher.find()) {
                    Toast.makeText(activity, "不支持输入表情", Toast.LENGTH_SHORT).show();
                    return "";
                }
                return null;
            }
        };
        cetYijianfankui.setFilters(new InputFilter[]{inputFilter, new InputFilter.LengthFilter(MAX_LENGTH)});
        cetYijianfankui.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                String content = cetYijianfankui.getText().toString();
                tvZishu.setText(content.length() + "/"
                        + MAX_LENGTH);
                if (cetYijianfankui.getText().length() == MAX_LENGTH) {
                    Toast.makeText(activity, "已到字数限制", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }


    @OnClick({R.id.iv_back, R.id.tv_fasong})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_fasong:
                Toast.makeText(activity, "发送成功", Toast.LENGTH_SHORT).show();
                cetYijianfankui.setText("");
                break;
        }
    }
}
